from django.db import DatabaseError
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler

def exception_handler(exc,context):
    """自定义DRF框架处理函数"""
    # 调用DRF框架的默认异常处理函数进行处理
    response = drf_exception_handler(exc,context)
    if not response:
        # DRF框架默认不能处理此异常
        if isinstance(exc,DatabaseError):
            response = Response({'message':'数据库异常'},
                                status=status.HTTP_507_INSUFFICIENT_STORAGE)
        return response
